package com.UnionPayCenter.operation.manager.impl;

import com.UnionPayCenter.mapper.OperationActivityMapper;
import com.UnionPayCenter.operation.entity.Activity;
import com.UnionPayCenter.operation.manager.ActivityManager;
import com.UnionPayCenter.service.ServiceTemplate;
import com.UnionPayCenter.util.Asserts;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OperationActivityManagerImpl implements ActivityManager {

    @Autowired
    private OperationActivityMapper operationActivityMapper;

    @Autowired
    private ServiceTemplate serviceTemplate;

    @Override
    public Integer addActivity(Activity operationActivity) {

        // 目前只有一个活动存在
        //List<OperationActivity> operationActivities = getByShopId(operationActivity.getShopId(), 1);
        //if (CollectionUtils.isNotEmpty(operationActivities)) {
        //    for (OperationActivity existed : operationActivities) {
        //        if (existed.getActivityTypeId().equals(operationActivity.getActivityTypeId())
        //                && existed.getRewardsType().equals(operationActivity.getRewardsType())) {
        //            ActivityErrorCodeEnum.ACTIVITY_EXISTED.exception();
        //        }
        //    }
        //}

        operationActivity.setCreateTime(new Date());
        operationActivity.setUpdateTime(new Date());
        operationActivity.setStatus(1);
        if (operationActivity.getThresholdStart() == null) {
            operationActivity.setThresholdStart(Integer.MIN_VALUE);
        }
        if (operationActivity.getThresholdEnd() == null) {
            operationActivity.setThresholdEnd(Integer.MAX_VALUE);
        }
        if (operationActivity.getEffectiveNextDay() == null) {
            operationActivity.setEffectiveNextDay(0);
        }
        operationActivityMapper.insert(operationActivity);
        return operationActivity.getActivityId();
    }

    @Override
    public Integer deleteById(Integer activityId) {
        return operationActivityMapper.deleteByPrimaryKey(activityId);
    }

    @Override
    public List<Activity> getByShopId(Integer shopId, Integer status) {
        Asserts.naturalNumber(shopId, "店铺Id");

        Activity operationActivity = new Activity();
        operationActivity.setShopId(shopId);
        operationActivity.setStatus(status);
        return operationActivityMapper.selectByCondition(operationActivity);
    }

    @Override
    public Activity getById(Integer activityId) {
        return operationActivityMapper.selectByPrimaryKey(activityId);
    }

    @Override
    public void updateActivity(Activity activity) {
        operationActivityMapper.updateByPrimaryKeySelective(activity);
    }

    @Override
    public Page<Activity> getByCondition(Activity activity, Integer page, Integer pageSize) {
        return serviceTemplate.pagination(page, pageSize,
                () -> operationActivityMapper.selectByCondition(activity));
    }
}
